class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    GetFilter() {
        let item = super.GetFilter(0);
        item["ItemNameFilterValue"] = $.trim($("#ItemNameFilterValue").val()).length > 0 ? $.trim($("#ItemNameFilterValue").val()) : null;

        return JSON.stringify(item);
    }
    FetchAll() {
        $("#ItemName").val("");
        this.FetchForPage(1);
    }
    SetTableContent(result) {
        $("#tabData01 tbody").empty();
        let content_01 = '';
        this.rows = result.length;
        this.numCheckbox = result.length;  // 设置表格中checkbox的数量为表格的行数
        for (let i = 0; i < result.length; i++) {
            let id = result[i].itemId;
            content_01 += '<tr _mash_id="' + id + '">';
            content_01 += '<td class="column__action"><span title="删除" class="trash" onclick="_Instance.DoDelete($(this));"><i class="fa-all fa-trash-o fa-1"></i></span></td>';
            content_01 += '<td class="edited-cell">' + result[i].itemNo + '</td>';
            content_01 += '<td class="edited-cell">' + result[i].itemName + '</td>';
            content_01 += '<td class="edited-cell">' + formatNullData(result[i].description) + '</td>';
            content_01 += '</tr>';

            $("#tabData01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tabData01");

        let id = "cell__field";
        let name = "_cell__field";
        $('#tabData01 tbody tr').find("td.edited-cell").click(function () {
            OnCellClickEvent($(this), id, name, 0);
        });

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    // 得到计量单位明细
    GetLinesData() {
        let jsonInsert = [];
        let jsonUpdate = [];
        let itemResult = {};
        let jsonResult = [];
        // 得到修改过数据
        $('#tabData01 tbody').find("tr").each(function () {
            let _ItemNo = $.trim($(this).find('td').eq(1).text()).length > 0 ? $.trim($(this).find('td').eq(1).text()) : null;   // 跟单项目编码
            let _ItemName = $.trim($(this).find('td').eq(2).text()).length > 0 ? $.trim($(this).find('td').eq(2).text()) : null; // 跟单项目名称
            let _Description = $.trim($(this).find('td').eq(3).text()).length > 0 ? $.trim($(this).find('td').eq(3).text()) : null;        // 备注

            if ($(this).find("td.dirty").length) {
                let item = {};
                let $row = $(this);

                item['itemId'] = $row.attr("_mash_id");
                item['itemNo'] = _ItemNo;
                item['itemName'] = _ItemName;
                item['description'] = _Description;
                item['createdBy'] = $("#UserId").val();
                item['updatedBy'] = $("#UserId").val();

                if (parseInt($row.attr("_mash_id")) === 0) {
                    jsonInsert.push(item);
                } else {
                    jsonUpdate.push(item);
                }
            }
        });
        itemResult['insertList'] = jsonInsert;
        itemResult['updateList'] = jsonUpdate;
        jsonResult.push(itemResult);

        return itemResult;
    }
    DoInsert() {
        this.InsertNewLines();
    }
    // 保存
    DoSave() {
        //step 1、进行页面验证，失败则返回
        let success_01 = CheckGridInput();
        if (!success_01)
            return;

        //step 2、执行处理
        if (this.GetLinesData().insertList.length > 0 || this.GetLinesData().updateList.length > 0) {
            super.DoAjax(this.GetLinesData(), "/master/po_tracking_item/save.html", function (data, $self) {
                if (data.LinesList !== undefined) {
                    $self.SetTableContent(data.LinesList);
                    ShowSuccessMessagePanel("保存成功。");
                }
            });
        }
    }
    DoDelete($self) {
        let _params = {};
        _params['ItemId'] = $self.parent().parent().attr("_mash_id");
        _params['ItemName'] = $self.parent().parent().find("td").eq(2).text();
        _params['DeletedBy'] = $("#UserId").val();

        super.DoDelete(_params, function ($self, data) {
            if (data.Success) {
                $self.SetTableContent(data.LinesList);
                ShowSuccessMessagePanel("删除成功。");
            }
        });
    }
    // 添加新行以及新行的操作方式
    InsertNewLines() {
        let $newLine = $('<tr class="newLine" _mash_id="0">' +
                '    <td style="text-align:center;"><span title="删除" class="trash" onclick="_Instance.RemoveNewLine($(this))"><i class="fa-all fa-trash-o fa-1"></i></span></td>' +
                '    <td class="edited-cell"></td>' +
                '    <td class="edited-cell"></td>' +
                '    <td class="edited-cell"></td>' +
                '</tr>');
        $('#tabData01 tbody').prepend($newLine);   // 加在前面
        this.rows = this.rows + 1;

        setOneTableRowColor("tabData01");
        this.AutoAdjustTableHeightForLines();  // 自适应高度设置

        let id = "cell__field";
        let name = "_cell__field";
        $newLine.find("td.edited-cell").click(function () {
            OnCellClickEvent($(this), id, name, 0);
        });
    }
    RemoveNewLine($self) {
        showModal(350, 140, '删除确认', '', null, null, 1, '请问，确定要删除当前行吗？', () => {
            $self.parent().parent().remove();
            this.rows = this.rows - 1;
            // 自适应高度设置
            this.AutoAdjustTableHeightForLines();
        });
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _tabTop = $("#divD1").offset().top;
        let _rc = Math.floor((h - _tabTop) / 25);
        let _dh = _rc * 25;
        let _tah = this.rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#divD1").css({"max-height": _dh, height: _dh});
    }
    SortTable(orderBy) {
        this.orderby = orderBy;
        // 得到排序数据
        this.FetchForPage(1);
    }
}

let _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/master/po_tracking_item/fetchAll.html",
            "/master/po_tracking_item/delete.html");
    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}

//-----------------------------------------------------------------------------
// 验证函数
//-----------------------------------------------------------------------------
function CheckGridInput() {
    $('#tabData01 tbody').find("tr").each(function () {
        let $row = $(this);
        let _ItemNo = $.trim($(this).find('td').eq(1).text());    // 跟单项目编码
        let _ItemName = $.trim($(this).find('td').eq(2).text());  // 跟单项目名称

        if (_ItemNo === '') {
            GridCheckErrHandle($row, 1, '请输入跟单项目编号。');
        } else {
            GridCheckSuccessHandle($row, 1);
        }
        if (_ItemName === '') {
            GridCheckErrHandle($row, 2, '请输入跟单项目名称。');
        } else {
            GridCheckSuccessHandle($row, 2);
        }

    });
    if ($('#tabData01 tbody').find('td.error').length > 0) {
        ShowErrorMessagePanel('对不起，填写的数据有错误。');
        return false;
    } else {
        return true;
    }
}
function GridCheckErrHandle($row, index, errMsg) {
    $row.find('td').eq(index).addClass("error");
    $row.find('td').eq(index).attr('title', errMsg);
}
function GridCheckSuccessHandle($row, index) {
    $row.find('td').eq(index).removeClass("error");
    $row.find('td').eq(index).attr('title', '');
}